package com.elinkway.infinitemovies.adaction.download;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

import com.apkfuns.logutils.LogUtils;
import com.elinkway.infinitemovies.async.ThirdAdReportTask;
import com.elinkway.infinitemovies.ui.activity.NewMainActivity;
import com.elinkway.infinitemovies.ui.activity.VideoDetailActivity;
import com.elinkway.infinitemovies.utils.UmengEventPoster;
import com.elinkway.infinitemovies.utils.Utils;

import java.util.ArrayList;

import static com.elinkway.infinitemovies.adaction.download.ReportADAppDownloadReceiver.ReportAppDownloadType.DOWNLOAD_SUCCESS;
import static com.elinkway.infinitemovies.adaction.download.ReportADAppDownloadReceiver.ReportAppDownloadType.INSTALL_SUCCESS;
import static com.elinkway.infinitemovies.adaction.download.ReportADAppDownloadReceiver.ReportAppDownloadType.START_DOWNLOAD;
import static com.elinkway.infinitemovies.adaction.download.ReportADAppDownloadReceiver.ReportAppDownloadType.START_INSTALL;

/**
 * 广告 app下载上报广播接受者
 */
public class ReportADAppDownloadReceiver extends BroadcastReceiver {
    /**
     * 广播发起时的上报类型 key，取值为ReportAppDownloadType中三种
     */
    public static final String REPORT_TYPE = "reportType";
    /**
    /**
     * 开始下载上报的url数组 key
     */
    public static final String DOWNLOAD_URLS = "downloadUrls";
    /**
     * 下载完成上报的url数组 key
     */
    public static final String DOWNLOADED_URLS = "downloadedUrls";
    /**
     * 开始安装上报的url数组 key
     */
    public static final String START_INSTALL_URLS = "startInstallUrls";
    /**
     * 安装完成上报的url数组 key
     */
    public static final String INSTALL_URLS = "installUrls";

    public static final String INNER_WEB_VIEW_TAG_START_DOWNLOAD = "ad_startdo";
    public static final String INNER_WEB_VIEW_TAG_DOWNLOAD_SUC = "ad_findo";
    public static final String INNER_WEB_VIEW_TAG_START_INSTALL = "ad_startins";
    public static final String INNER_WEB_VIEW_TAG_INSTALL_SUC = "ad_finins";
    public static final String INNER_WEB_VIEW_KEY_POSITION = "position";
    public static final String INNER_WEB_VIEW_FROM_HOME = "homecircle";
    public static final String INNER_WEB_VIEW_FROM_HALF = "detailact";

    public ReportADAppDownloadReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent != null){
            int reportType = intent.getIntExtra(REPORT_TYPE, 0);
            String adPos = intent.getStringExtra(Utils.AD_POS);
            ArrayList<String> reportUrls = null;
            switch (reportType){
                case START_DOWNLOAD:
                    reportUrls = intent.getStringArrayListExtra(ReportADAppDownloadReceiver.DOWNLOAD_URLS);
                    break;
                case DOWNLOAD_SUCCESS:
                    reportUrls = intent.getStringArrayListExtra(ReportADAppDownloadReceiver.DOWNLOADED_URLS);
                    break;
                case INSTALL_SUCCESS:
                    reportUrls = intent.getStringArrayListExtra(ReportADAppDownloadReceiver.INSTALL_URLS);
                    break;
                case START_INSTALL:
                    reportUrls = intent.getStringArrayListExtra(ReportADAppDownloadReceiver.START_INSTALL_URLS);
                    break;
                default:
                    break;
            }
            if(reportUrls != null && reportUrls.size() > 0){
                for (int i=0;i<reportUrls.size();i++){
                    if(!TextUtils.isEmpty(reportUrls.get(i))){
                        new ThirdAdReportTask(context, reportUrls.get(i)).start();
                    }
                }
            }
            if (adPos != null) {
                LogUtils.e("inner upload download adPos:" + adPos + " reportType：" + reportType);
                if (reportType == ReportAppDownloadType.INNER_WEBVIEW_START_DOWNLOAD) {
                    UmengEventPoster.doPost(INNER_WEB_VIEW_TAG_START_DOWNLOAD, INNER_WEB_VIEW_KEY_POSITION, adPos);
                } else if (reportType == ReportAppDownloadType.INNER_WEBVIEW_DOWNLOAD_SUCCESS) {
                    UmengEventPoster.doPost(INNER_WEB_VIEW_TAG_DOWNLOAD_SUC, INNER_WEB_VIEW_KEY_POSITION, adPos);
                } else if (reportType == ReportAppDownloadType.INNER_WEBVIEW_INSTALL_SUCCESS) {
                    UmengEventPoster.doPost(INNER_WEB_VIEW_TAG_INSTALL_SUC, INNER_WEB_VIEW_KEY_POSITION, adPos);
                } else if (reportType == ReportAppDownloadType.INNER_WEBVIEW_START_INSTALL) {
                    UmengEventPoster.doPost(INNER_WEB_VIEW_TAG_START_INSTALL, INNER_WEB_VIEW_KEY_POSITION, adPos);
                }
            }
        }
    }

    public interface ReportAppDownloadType {
        int START_DOWNLOAD = 1;
        int DOWNLOAD_SUCCESS = 2;
        int INSTALL_SUCCESS = 3;
        int START_INSTALL = 4;
        int INNER_WEBVIEW_START_DOWNLOAD = 5;
        int INNER_WEBVIEW_DOWNLOAD_SUCCESS = 6;
        int INNER_WEBVIEW_INSTALL_SUCCESS = 7;
        int INNER_WEBVIEW_START_INSTALL = 8;
    }
}
